from PySide6.QtCore import  Property, Qt,QObject, Slot, Signal,QAbstractTableModel,QAbstractItemModel
from PySide6.QtQml import  QmlElement
from PySide6.QtWidgets import QFileDialog
from PySide6.QtCore import QTimer
from time import strftime, localtime
import json
import datetime
import logging
import random
import os
import platform
import traceback
from openpyxl import load_workbook
import re
import logging
QML_IMPORT_NAME = "QBridge"
QML_IMPORT_MAJOR_VERSION = 1
class PandasModel(QAbstractTableModel):
    def __init__(self, data):
        QAbstractTableModel.__init__(self)
        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parnet=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None       
@QmlElement
class Bridge(QObject):
    updated = Signal(str, arguments=['time'])
    def __init__(self):
        super().__init__()
        # Define timer.
        self.timer = QTimer()
        self.timer.setInterval(1000)  # msecs 100 = 1/10th sec
        self.timer.timeout.connect(self.update_time)
        self.timer.start()

    modelChanged = Signal()
    # @pyqtProperty(Person, notify=hostChanged)
    @Property(QObject, notify=modelChanged)
    def model(self):
        return self._model

    @Slot(str,result=QAbstractTableModel)
    def getfilepath(self,filepath):
        self.datafilepath =  filepath
        df = ppdd.readdatafile(filepath)
        self.pandasmodel = PandasModel(df)
        return self.pandasmodel    
    @Slot(str,result=QAbstractTableModel)
    def import_file(self,filename):
        self.datafilepath =  filepath
        df = ppdd.readdatafile(filepath)
        self.pandasmodel = PandasModel(df)
        return self.pandasmodel    
    @Slot(str,str,int,int,result=str)
    def getContacts(self,search="", baoxiang="",start=0,limit=10):
        data=[{"id":"1","num":0,"mass":0.1},{"num":1,"id":"2","mass":0.2}]
        return json.dumps(data)
    def update_time(self):
        curr_time = strftime("%H:%M:%S", localtime())
        self.updated.emit(curr_time)

if __name__ == '__main__':
    b=Bridge();
    b.getPacks("{}");
